﻿Imports WINX32Lib 'Remember to add r3eference first (Add Reference option). For MS Visual Studio .NET 2003: before adding a reference to the project, corresponding assembly should be added to the Add Reference dialog box (http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.71%29.aspx).
Imports System.IO
Imports System.Math

Module Functions

    Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" _
        (ByVal Destination As Long, ByVal Source As Long, ByVal Length As Long)

    'Save_Each must divie both: Total_Number_Of_Frames and Max_No_Of_Frames_Per_File
    Public Sub Acquisition(ByVal Total_Number_Of_Frames As Integer, ByVal Max_No_Of_Frames_Per_File As Integer, ByVal Save_Each As Integer, _
                         ByVal File_Path As String, ByVal File_Name As String, ByVal Acq_Setup As ExpSetup)

        Dim Number_Of_Files As Integer = Ceiling(Total_Number_Of_Frames / Max_No_Of_Frames_Per_File)
        Dim Acqusition_Running As Boolean = True

        For i As Integer = 0 To Number_Of_Files - 1 'The first loop does hard saves of files (each with Max_No_Of_Frames_Per_File frames)
            Dim Final_Number_Of_Frames_In_File As Integer = Min(Max_No_Of_Frames_Per_File, Total_Number_Of_Frames - i * Max_No_Of_Frames_Per_File)
            Dim Number_Of_File_Saves As Integer = Ceiling(Final_Number_Of_Frames_In_File / Save_Each)
            Dim Acq_File As New DocFile

            If i = 0 Then
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & ".spe")
            Else
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & i & ".spe")
            End If

            For j As Integer = 0 To Number_Of_File_Saves - 1 'The second loop fills each file gradually (by Save_Each frames)

                If j = 0 Then
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 1) 'Overwrite
                Else
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 2) 'Append to
                End If

                'Me.Hide()
                Acq_Setup.Start(Acq_File)

                Dim Number_Of_Frames_In_File As Integer = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) 'the number of frames in Acq_File after appending frames by Acq_Setup.Start(Acq_File) 



                While Acq_Setup.GetParam(EXP_CMD.EXP_RUNNING_EXPERIMENT) = 1
                End While

                'Terminating the acquisition by user changes the DM_CMD.DM_NUMFRAMES parameter
                If Not Number_Of_Frames_In_File = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) Then
                    Acqusition_Running = False
                    Exit For
                End If

                'Me.Show()
            Next
            If Not Acqusition_Running Then
                'Me.Show()
                Exit For
            End If
            Acq_File.Close() 'Not closing files while creating others causes virtual memory to run out of space - soon MapViewOfFile interruption is likely to occur (e.g. after 1100 frames acquired in total)
        Next

    End Sub

    'Acquisition that take arbitrary valueas of Total_Number_Of_Frames Max_No_Of_Frames_Per_File  Save_Each
    Public Sub Acquisition1(ByVal Total_Number_Of_Frames As Integer, ByVal Max_No_Of_Frames_Per_File As Integer, ByVal Save_Each As Integer, _
                        ByVal File_Path As String, ByVal File_Name As String, ByVal Acq_Setup As ExpSetup)

        Dim Number_Of_Files As Integer = Ceiling(Total_Number_Of_Frames / Max_No_Of_Frames_Per_File)
        Dim Acqusition_Running As Boolean = True

        'Dim Spectrum_File As New DocFile
        ''Dim Good_Events_Per_Frame_File As New DocFile
        ''Dim Good_Events_File As New DocFile
        ''Dim Histogram_File As New DocFile
        'Spectrum_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), 1, 1, dataType.X_LONG, File_Path & "\" & File_Name & "s.spe")
        'Good_Events_Per_Frame_File.Open("", Total_Number_Of_Frames, 1, 1, dataType.X_LONG, File_Path & "\" & File_Name & "l.spe")
        'Good_Events_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), Acq_Setup.GetParam(EXP_CMD.EXP_YDIMDET), 1, dataType.X_LONG, File_Path & "\" & File_Name & "o.spe")
        ''Histogram_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), 1, 1, dataType.X_LONG, File_Path & "\" & File_Name & "s.spe")

        'To initialize the output spectras of analize we need to invoke the Analyze function 
        Analyze2(Nothing, Total_Number_Of_Frames)

        For i As Integer = 0 To Number_Of_Files - 1 'The first loop does hard saves of files (each with Max_No_Of_Frames_Per_File frames)
            Dim Final_Number_Of_Frames_In_File As Integer = Min(Max_No_Of_Frames_Per_File, Total_Number_Of_Frames - i * Max_No_Of_Frames_Per_File)
            Dim Number_Of_File_Saves As Integer = Ceiling(Final_Number_Of_Frames_In_File / Save_Each)

            Dim Acq_Setup2 As New ExpSetup 'to change the EXP_CMD.EXP_SEQUENTS parameter the creation of new ExpSetup is neccessary
            Acq_Setup2.SetParam(EXP_CMD.EXP_SEQUENTS, Min(Save_Each, Final_Number_Of_Frames_In_File))


            Dim Acq_File As New DocFile ' we want the dafaoult parameters to be load fo this the Acq_Setup needs to be defined

            If i = 0 Then
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & ".spe")
            Else
                Acq_File.Open("", 0, 0, 0, Acq_Setup.GetParam(EXP_CMD.EXP_DATATYPE), File_Path & "\" & File_Name & i & ".spe")
            End If



            For j As Integer = 0 To Number_Of_File_Saves - 1 'The second loop fills each file gradually (by Save_Each frames)

                Dim Number_Of_Sequences As Integer

                If (j + 1) * Save_Each < Final_Number_Of_Frames_In_File Then
                    Number_Of_Sequences = Save_Each
                Else
                    Number_Of_Sequences = Final_Number_Of_Frames_In_File - (Number_Of_File_Saves - 1) * Save_Each
                End If


                If j = 0 Then
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 1) 'Overwrite
                Else
                    Acq_Setup.SetParam(EXP_CMD.EXP_FILEACCESS, 2) 'Append to
                End If

                Dim Acq_Setup3 As New ExpSetup 'to change the EXP_CMD.EXP_SEQUENTS parameter the creation of new ExpSetup is neccessary
                Acq_Setup3.SetParam(EXP_CMD.EXP_SEQUENTS, Number_Of_Sequences)

                Acq_Setup.Start(Acq_File)

                Dim Number_Of_Frames_In_File As Integer = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) 'the number of frames in Acq_File after appending frames by Acq_Setup.Start(Acq_File) 
                Dim No_Of_Frames_Analized As Integer = Acq_File.GetParam(DM_CMD.DM_LASTFRAMERDY)
                While (Acq_Setup.GetParam(EXP_CMD.EXP_RUNNING_EXPERIMENT) = 1) Or (Acq_File.GetParam(DM_CMD.DM_LASTFRAMERDY) > No_Of_Frames_Analized)
                    '    'Dim LASTFRAMERDY As Integer = Acq_File.GetParam(DM_CMD.DM_LASTFRAMERDY)
                    If Acq_File.GetParam(DM_CMD.DM_LASTFRAMERDY) > No_Of_Frames_Analized Then
                        Dim Frame_To_Analyze As Object = Nothing
                        Acq_File.GetFrame(No_Of_Frames_Analized + 1, Frame_To_Analyze)
                        Analyze2(Frame_To_Analyze, Total_Number_Of_Frames)
                        No_Of_Frames_Analized = No_Of_Frames_Analized + 1
                    End If
                End While

                'Terminating the acquisition by user changes the DM_CMD.DM_NUMFRAMES parameter
                If Not Number_Of_Frames_In_File = Acq_File.GetParam(DM_CMD.DM_NUMFRAMES) Then
                    Acqusition_Running = False
                    Exit For
                End If
            Next
            If Not Acqusition_Running Then
                Exit For
            End If
            Acq_File.Close() 'Not closing files while creating others causes virtual memory to run out of space - soon MapViewOfFile interruption is likely to occur (e.g. after 1100 frames acquired in total)
        Next

    End Sub

    Sub Analyze2(ByVal Frame_To_Analyze As Array, ByVal Total_Number_Of_Frames As Integer, _
                Optional ByVal low As Short = 1, Optional ByVal high As Short = 2, Optional ByVal low1 As Short = 1, Optional ByVal high1 As Short = 2)
        Dim tic, toc As String
        tic = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60

        Static Dim Spectrum_File As New DocFile
        Static Dim Total_Number_Of_Good_Events_Per_Frame_File As New DocFile
        Static Dim Good_Events_File As New DocFile
        Static Dim Histogram_File As New DocFile
        Static Dim Multiple_Hits_File As New DocFile
        Static Dim Total_Number_Of_Multiple_Hits_File As New DocFile
        Static Dim Spectrum(,) As Integer
        Static Dim Good_Events()(,) As Integer
        Static Dim Multiple_Hits()(,) As Integer
        Static Dim Intensity_Histograme()() As Integer
        Static Dim Frame_Analized As Integer = 0

        If Spectrum_File.GetParam(DM_CMD.DM_FILETITLE) Is Nothing Then
            Dim Acq_Setup As New ExpSetup
            Dim Spectrum_File_Path As String = Path.GetDirectoryName(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
            Dim Spectrum_File_Name As String = Path.GetFileNameWithoutExtension(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
            Total_Number_Of_Good_Events_Per_Frame_File.Open("", Total_Number_Of_Frames, 2, 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "l.spe")
            Spectrum_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), 2, 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "s.spe")
            Good_Events_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), Acq_Setup.GetParam(EXP_CMD.EXP_YDIMDET), 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "o.spe")
            Multiple_Hits_File.Open("", Acq_Setup.GetParam(EXP_CMD.EXP_XDIMDET), Acq_Setup.GetParam(EXP_CMD.EXP_YDIMDET), Floor(low / (high - low)) + 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "mo.spe")
            Total_Number_Of_Multiple_Hits_File.Open("", Total_Number_Of_Frames, Floor(low / (high - low)) + 1, 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "ml.spe")
        End If


        If Frame_To_Analyze Is Nothing Then
            Frame_Analized = 0
            Spectrum = Nothing
            Good_Events = Nothing
            Multiple_Hits = Nothing
            Intensity_Histograme = Nothing

            'Clear_DocFile(Spectrum_File)
            'Clear_DocFile(Total_Number_Of_Good_Events_Per_Frame_File)
            'Clear_DocFile(Good_Events_File)
            'Clear_DocFile(Histogram_File)
            Exit Sub
        Else
            Frame_Analized = Frame_Analized + 1
            'Dim Spectrum_addition(,) As Integer
            'Dim Good_Events_addition(,,) As Short
            'Dim Total_Number_Of_Good_Events_addition() As Integer
            'Dim Intensity_Histograme_addition(,) As Short
            'Dim Multiple_Hits_addition(,,) As Byte
            'Dim Total_Number_Of_Multiple_Hits_addition() As Short = Nothing

            Dim Total_Number_Of_Good_Events() As Integer
            Dim Total_Number_Of_Multiple_Hits() As Integer
            toc = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60 - tic
            Dim toc2 As String
            tic = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60

            '%%%%%%%%%%%%%%%%%%%%%%
            'Strange behaviour - the function call takes more than 1s
            Analyze_Frame(Frame_To_Analyze, 1, 2, 1, 2, _
                          Spectrum, Good_Events, _
                          Total_Number_Of_Good_Events, Intensity_Histograme, _
                          Multiple_Hits, Total_Number_Of_Multiple_Hits, toc2)
            toc = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60 - tic
            tic = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60
            '%%%%%%%%%%%%%%%%%%


            Spectrum_File.PutFrame(1, Spectrum)
            Good_Events_File.PutFrame(1, Good_Events(0))
            Good_Events_File.PutFrame(1, Good_Events(1))
            Total_Number_Of_Good_Events_Per_Frame_File.PutPixel(1, 1, Frame_Analized, Total_Number_Of_Good_Events(0))
            Total_Number_Of_Good_Events_Per_Frame_File.PutPixel(1, 2, Frame_Analized, Total_Number_Of_Good_Events(1))

            For i As Integer = 0 To Floor(low / (high - low))
                'Dim tem_Multiple_Hits(Multiple_Hits.GetLength(0) - 1, Multiple_Hits.GetLength(1) - 1) As Array
                'Array.Copy(Multiple_Hits, i * tem_Multiple_Hits.Length, tem_Multiple_Hits, 0, tem_Multiple_Hits.Length)
                Multiple_Hits_File.PutFrame(i + 1, Multiple_Hits(i))
                Total_Number_Of_Multiple_Hits_File.PutPixel(1, i + 1, Frame_Analized, Total_Number_Of_Multiple_Hits(i))
            Next

            If Frame_Analized = Total_Number_Of_Frames Then
                Dim Acq_Setup As New ExpSetup
                Dim Spectrum_File_Path As String = Path.GetDirectoryName(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
                Dim Spectrum_File_Name As String = Path.GetFileNameWithoutExtension(Acq_Setup.GetParam(EXP_CMD.EXP_DATFILENAME))
                Dim Final_Length As Integer = Array.FindLastIndex(Intensity_Histograme(0), AddressOf isNotZero)
                Histogram_File.Open("", Final_Length + 1, 2, 1, dataType.X_LONG, Spectrum_File_Path & "\" & Spectrum_File_Name & "h.spe")
                ReDim Preserve Intensity_Histograme(0)(Final_Length)
                ReDim Preserve Intensity_Histograme(1)(Final_Length)
                Dim test(,) As Integer
                ReDim test(Final_Length, 1)
                CopyMemory(test(Final_Length, 0), Intensity_Histograme(0)(0), Final_Length * 4)
                CopyMemory(test(Final_Length, 1), Intensity_Histograme(0)(0), Final_Length * 4)
                Histogram_File.PutFrame(1, test)
            End If
            toc = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60 - tic
        End If


    End Sub

    Function isNotZero(ByVal a As Integer) As Boolean
        Return Not a.Equals(0)
    End Function

    Sub Clear_DocFile(ByRef Doc_File As DocFile)
        For i As Integer = 1 To Doc_File.GetParam(DM_CMD.DM_NUMFRAMES)
            Dim Temporary_Frame As Array
            Doc_File.GetFrame(i, Temporary_Frame)
            Array.Clear(Temporary_Frame, 0, Temporary_Frame.Length)
            Doc_File.PutFrame(i, Temporary_Frame)
        Next
    End Sub

    'Sub Add_to_DocFile(ByRef Doc_File As DocFile, ByVal addition As Array)
    '    For i As Integer = 1 To Doc_File.GetParam(DM_CMD.DM_NUMFRAMES)
    '        Dim Temporary_Frame As Array
    '        Doc_File.GetFrame(i, Temporary_Frame)
    '        For j As Integer = 0 To Temporary_Frame.Length - 1
    '            Temporary_Frame.SetValue(Temporary_Frame.GetValue(j) + addition.GetValue(j + i * Temporary_Frame.Length), j)
    '        Next
    '        Doc_File.PutFrame(i, Temporary_Frame)
    '    Next
    'End Sub



    Sub Analyze_Frame(ByRef Frame_To_Analyze As Array,
                 ByVal low As Short, ByVal high As Short, ByVal low1 As Short, ByVal high1 As Short, _
                 Optional ByRef Spectrum(,) As Integer = Nothing, _
                 Optional ByRef Good_Events_Sum()(,) As Integer = Nothing, _
                 Optional ByRef Total_Number_Of_Good_Events() As Integer = Nothing, _
                 Optional ByRef Intensity_Histograme()() As Integer = Nothing, _
                 Optional ByRef Multiple_Hits()(,) As Integer = Nothing, _
                 Optional ByRef Total_Number_Of_Multiple_Hits() As Integer = Nothing, Optional ByRef toc2 As String = "")

        Dim tic As String
        tic = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60

        If Not Frame_To_Analyze.Rank = 2 Then
            MsgBox("The frame to analize hase a dimention of " & Frame_To_Analyze.Rank)
        End If

        Dim Max_Multiplicity As Integer = low \ (high - low)
        Static Dim Max_Intensity As Integer = Max_Multiplicity


        'Dim Spectrum(,) As Short
        'Dim Multiple_Hits(,,) As Byte
        Dim Good_Events(,,) As Integer
        ReDim Good_Events(Frame_To_Analyze.GetLength(0), Frame_To_Analyze.GetLength(1), 1)
        'Dim Intensity_Histograme(,) As Short

        ReDim Preserve Spectrum(Frame_To_Analyze.GetLength(0) - 1, 1)
        ReDim Preserve Multiple_Hits(Max_Multiplicity)
        For i As Integer = 0 To Max_Multiplicity
            ReDim Preserve Multiple_Hits(i)(Frame_To_Analyze.GetLength(0) - 1, Frame_To_Analyze.GetLength(1) - 1)
        Next
        ReDim Total_Number_Of_Multiple_Hits(Max_Multiplicity)
        ReDim Preserve Good_Events_Sum(1)
        ReDim Preserve Good_Events_Sum(0)(Frame_To_Analyze.GetLength(0) - 1, Frame_To_Analyze.GetLength(1) - 1)
        ReDim Preserve Good_Events_Sum(1)(Frame_To_Analyze.GetLength(0) - 1, Frame_To_Analyze.GetLength(1) - 1)
        ReDim Total_Number_Of_Good_Events(1)
        ReDim Preserve Intensity_Histograme(1)
        ReDim Preserve Intensity_Histograme(0)(Max_Intensity)
        ReDim Preserve Intensity_Histograme(1)(Max_Intensity)
        'toc = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60 - tic
        'tic = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60
Start_of_multiple_hits_analysis:
        Dim Temp_Pixel As Integer
        For i As Integer = 0 To Frame_To_Analyze.GetLength(0) - 1
            For j As Integer = 0 To Frame_To_Analyze.GetLength(1) - 1
                Temp_Pixel = Frame_To_Analyze(i, j)
                If Max_Intensity < Temp_Pixel Then
                    Max_Intensity = Temp_Pixel * 2
                    ReDim Preserve Intensity_Histograme(0)(Max_Intensity)
                    ReDim Preserve Intensity_Histograme(1)(Max_Intensity)
                End If
                Intensity_Histograme(0)(Temp_Pixel) = Intensity_Histograme(0)(Temp_Pixel) + 1
                Intensity_Histograme(1)(Temp_Pixel) = Intensity_Histograme(1)(Temp_Pixel) + 1
                'here we check the multiplicity of the hit
                Dim Lower_Lewel_Multiplicity As Integer = Temp_Pixel \ low
                If Lower_Lewel_Multiplicity > 0 Then 'one should be creful to satisfy the condition : high < 2*low
                    Dim Upper_Lewel_Multiplicity As Integer = Ceiling(Temp_Pixel / high)
                    If Lower_Lewel_Multiplicity >= Upper_Lewel_Multiplicity Then
                        Good_Events(i, j, 0) = Lower_Lewel_Multiplicity
                        Total_Number_Of_Good_Events(0) += Lower_Lewel_Multiplicity
                        'Total_Number_Of_Good_Events(1) += Lower_Lewel_Multiplicity
                        Frame_To_Analyze(i, j) = 0
                        If Lower_Lewel_Multiplicity = Max_Multiplicity Then
                            Multiple_Hits(Lower_Lewel_Multiplicity)(i, j) += 1
                            Total_Number_Of_Multiple_Hits(Lower_Lewel_Multiplicity) += 1
                        Else
                            Good_Events(i, j, 1) = Upper_Lewel_Multiplicity - Lower_Lewel_Multiplicity
                            Total_Number_Of_Good_Events(1) += Good_Events(i, j, 1)
                        End If
                    Else
                        GoTo End_of_neighbours_analysis
                    End If
                End If
Start_of_neghbours_analysis:
                If j > 0 AndAlso i > 0 Then
                    Dim Neighbours_Sum As Integer = Frame_To_Analyze(i - 1, j - 1) + Frame_To_Analyze(i - 1, j) + Frame_To_Analyze(i, j - 1) + Frame_To_Analyze(i, j)
                    If (Neighbours_Sum > low1) AndAlso (Neighbours_Sum < high1) Then
                        Intensity_Histograme(0)(Neighbours_Sum) = Intensity_Histograme(0)(Neighbours_Sum) + 1
                        Intensity_Histograme(1)(Frame_To_Analyze(i - 1, j - 1)) = Intensity_Histograme(1)(Frame_To_Analyze(i - 1, j - 1)) + (Not CBool(Good_Events(i - 1, j - 1, 0)))

                        'Frame_To_Analyze(i - 1, j - 1) = 0 'not needed
                        Intensity_Histograme(1)(Frame_To_Analyze(i, j - 1)) = Intensity_Histograme(1)(Frame_To_Analyze(i, j - 1)) + (Not CBool(Good_Events(i, j - 1, 0)))
                        Frame_To_Analyze(i, j - 1) = 0
                        Intensity_Histograme(1)(Frame_To_Analyze(i - 1, j)) = Intensity_Histograme(1)(Frame_To_Analyze(i - 1, j)) + (Not CBool(Good_Events(i - 1, j, 0)))
                        Frame_To_Analyze(i - 1, j) = 0
                        Intensity_Histograme(1)(Frame_To_Analyze(i, j)) = Intensity_Histograme(1)(Frame_To_Analyze(i, j)) + (Not CBool(Good_Events(i, j, 0)))
                        Frame_To_Analyze(i, j) = 0

                        Multiple_Hits(0)(i, j) += 1
                        Total_Number_Of_Multiple_Hits(0) += 1
                        Good_Events(i, j, 0) = Good_Events(i, j, 0) + 1
                        Total_Number_Of_Good_Events(0) += 1
                        'Total_Number_Of_Good_Events(1) += 1
                    End If
                End If
End_of_neighbours_analysis:
                Spectrum(i, 0) = Spectrum(i, 0) + Good_Events(i, j, 0)
                Spectrum(i, 1) = Spectrum(i, 1) + Good_Events(i, j, 1)
                Good_Events_Sum(0)(i, j) = Good_Events_Sum(0)(i, j) + Good_Events(i, j, 0)
                Good_Events_Sum(1)(i, j) = Good_Events_Sum(1)(i, j) + Good_Events(i, j, 1)
            Next
        Next
        toc2 = (Now().Millisecond) / 1000 + Now().Second + (Now().Minute) * 60 - tic
    End Sub








End Module
